<?php 
namespace Admin\Controller;

class NodeController extends PublicController
{
	// 定义构造方法,显示当前模块标题
	public function __construct()
	{
		parent::__construct();
		$this->assign('title','权限管理');
	}

	// 显示默认所有权限列表
	public function index()
	{
		// 得到查询分页的条件
		$map = array();
		if (!empty($_GET['node'])) {
			$map['node'] = array('LIKE',"%{$_GET['node']}%");
		}
		foreach ($map as $key => $value) {
			// 给Page类加参数的另一种方法
			$p->parameter .= "$key=" . urlencode($val) . "&";
		}

		// 页面分页类
		$node = M('Node'); // 实例化node对象
		
		$count = $node->where($map)->count();// 查询满足要求的总记录数
		$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)

		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$nodelist = $node->where($map)->order()->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('page',$show);// 赋值分页输出

		$this->assign('nodelist',$nodelist);
		$this->assign('locat','节点列表');
		$this->display('Node/index');
	}

	// 加载添加节点页面
	public function add()
	{
		$this->assign('locat','添加节点');
		$this->display('Node/add');
	}

	// 执行添加
	public function insert()
	{
		$node = M('node');
		// 检测节点是否存在
		$where['node'] = $_POST['nodename'];
		if($node->where($where)->find()){
			$this->error('该节点已存在!');
			exit;
		}
		// 执行添加
		$data['node'] = $_POST['nodename'];
		$data['nodedetail'] = $_POST['nodedetail'];
		$data['controller'] = $_POST['controller'];
		$data['method'] = $_POST['method'];
		$result = $node->data($data)->add();
		if ($result) {
			$this->success('添加成功!','index');
		}else{
			$this->error('添加失败...');
			exit;
		}
	}

	// 删除节点
	public function del()
	{
		$id = I('get.nodeid');
		$node = M('node');
		$where['nodeid'] = $id;
		if($node->where($where)->delete()){
			$this->success('删除成功!');
		}else{
			$this->error('删除失败...');
			exit;
		}
	}

	// 加载编辑节点页面
	public function edit()
	{
		$node = M('node');
		$id = I('get.nodeid');
		$where['nodeid'] = $id;
		$nodedata = $node->where($where)->find();
		$this->assign('locat','编辑节点');
		$this->assign('nodedata',$nodedata);
		$this->display('Node/edit');
	}

	// 更新数据
	public function update()
	{
		$nodetable = M('node');

		// 检测修改的角色名是否已经存在
		$nodeid = I('post.nodeid');
		$node = I('post.nodename');
		$map['node'] = array('eq',$node);
		$map['nodeid'] = array('neq',$nodeid);
		if ($nodetable->where($map)->find()) {
			$this->error('修改的节点已存在...');
			exit;
		}

		// 执行修改
		// dump($_POST);
		$data['nodeid'] = $nodeid;
		$data['node'] = $_POST['nodename'];
		$data['nodedetail'] = $_POST['nodedetail'];
		$data['controller'] = $_POST['controller'];
		$data['method'] = $_POST['method'];
		$result = $nodetable->data($data)->save();

		if($result == 1){
			$this->success('修改成功!','index');
		}else if($result === false){
			$this->error('修改失败...');
			exit;
		}else{
			$this->error('您没有进行任何修改!');
			exit;
		}

	}
}